<template>
    <view class="tabview">
        <up-subsection :list="list" :duration="100" :current="0" mode="subsection" font-size="16" @change="change"></up-subsection>
    </view>
   <view class="list_container">
         <up-list
                     @scrolltolower="scrolltolower"
                 >
                     <up-list-item
                         v-for="(item, index) in indexList"
                         :key="index"
                         :height="winHeight-106"
                     >
                   <MyOffer v-bind="item"/>
                     </up-list-item>
                 </up-list>
     </view>
     <view>
       <OfferWin v-if="isShowOfferWin" v-bind="winProps"/>
       <up-popup :show="showPop" mode="center"  @close="closePop">
        <view>
          <up-image :show-loading="true" :src="qrUrl" width="250px" height="300px" ></up-image>
        </view>
	     </up-popup>
     </view>
     
</template>

<script setup lang="ts">
import { useCurrentServe } from '@/stores/models/server';
import { http } from '@/utils/http';
import type { OfferWinProps, MyOfferProps, ResOfferInfoData, OhterOfferProps, ResUserProfileData } from '@/utils/page_types';
import { onLoad } from '@dcloudio/uni-app';
import {ref,reactive, onBeforeMount, onMounted} from 'vue';

const isShowOfferWin = ref(false)
const indexList = ref<OhterOfferProps[]>([]);
const list = ref(['已接受','已拒绝','未处理']);
const winProps = ref<OfferWinProps>({} as OfferWinProps)
const winHeight = ref();
const currentPage = ref(1)
const serverStore = useCurrentServe()
const tabIndex = ref(2)
const qrUrl = ref('');
const showPop = ref(false)
const closePop=()=>{
    showPop.value =false
}
const openPop=async (infoUser:string)=>{
     console.log("infouser",infoUser)
     showPop.value =true;
    const data = await http<ResUserProfileData>({
		url:"/user",
		data:{
			openId:infoUser
		},
		method:"GET"
	 });
   const baseUrl ='http://api.stone09.com:8080/api/v1'
   if(data.code==1000){
    qrUrl.value =baseUrl+  data.data.WxQRCode;
    console.log("qrUrl.value",qrUrl.value)
   }
}

  onLoad(()=>{
    winHeight.value=uni.getSystemInfoSync().windowHeight;
   })

 onMounted(()=>{
  loadMore();
 })


 

const onWinClose=()=>{
   isShowOfferWin.value=false;
};
const onWinOpen=()=>{
   isShowOfferWin.value=true;
}



const onOffer=(infoId:string,infoOpenId:string,type:number)=>{
   if(isShowOfferWin.value){
       return
   }else{
       isShowOfferWin.value=true;
       winProps.value!.infoId=infoId
       winProps.value.infoOpenId = infoOpenId
       winProps.value.type = type
       winProps.value.onClose=onWinClose
       winProps.value.onOpen=onWinOpen
   
   }
    console.log("infoID",infoId);
}

const scrolltolower = () => {
  loadMore();
};

const loadMore=async ()=>{
    console.log("loadReceiveOffer",serverStore.serverId)
   const data=await http<Array<ResOfferInfoData>>({
		url:"/offerByUser",
		data:{
			page:currentPage.value,
			size:20,
			severId:serverStore.serverId,
      status:tabIndex.value
		},
		method:"GET"
	 });
	 console.log("data",data)
	 const list =data.data;
     if(!list) return;
   
     for(let i=0;i<list.length;i++){
		let infoData= list[i];
		let obj:OhterOfferProps ={
            id:infoData.sdInfoId,
         title:infoData.title,
		 userName:infoData.openId,
		 updateTime:infoData.updateAt.toString(),
		 price:infoData.price.toString(),
		 num:infoData.num.toString(),
	     otherNum:infoData.offer_num.toString(),
         otherPrice:infoData.offer_price,
         offerId:infoData.offer_id,
         infoId:infoData.sdInfoId,
         dealUser:infoData.openId,
         offerUser:infoData.offer_user,
         status:infoData.offer_status.toString(),
         type:infoData.type,
         initList:initList,
         reOffer:onOffer,
         contact:openPop

	}
	indexList.value.push(obj)
	 }
	 currentPage.value++;
 }



 const initList=()=>{
    currentPage.value=1;
    indexList.value.length=0;
    loadMore();
 }





// 0 未处理  1  已拒绝  2 已接受 ' 
const change=(index:number)=>{
   console.log('index ', index ); 
   switch(index){
    case 0:
    tabIndex.value=2;
    break;
      case 1:
      tabIndex.value=1;
      break;
        case 2:
        tabIndex.value=0;
        break;
   }
   initList()
}	
   
</script>

<style lang="scss">
   .list_container{
       display:flex;
       flex-direction: column;
       align-items: center;
   }
   page{
       background-color: #f6f6f6;
   }
   .tabview{
       width: 95%;
       margin: 0 auto;
       margin-top: 3px
   }
</style>
